﻿// See https://aka.ms/new-console-template for more information
using Microsoft.Web.Administration;
using ReloadSite;
using System.Net;
var url = "";
if (System.IO.File.Exists(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "appsettings.json")))
{
    var txt = System.IO.File.ReadAllText(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "appsettings.json"));
    var tx = Newtonsoft.Json.JsonConvert.DeserializeObject<AppsettingModel>(txt);
    url = tx?.url;
}
if (string.IsNullOrWhiteSpace(url))
{
    Console.WriteLine("未配置IP地址");
    return;
}
Console.WriteLine("重启RMIS");
using var serverManager = new ServerManager();
var siteName = "HWKJ.RMIS.Web";
var site = serverManager.Sites[siteName];
if (site == null)
{
    Console.WriteLine($"站点 '{siteName}' 不存在。");
    return;
}

try
{
    ApplicationPool appPool = serverManager.ApplicationPools[siteName];
    if (appPool != null)
    {
        // 重启应用程序池
        appPool.Recycle();
        Console.WriteLine($"应用程序池 {siteName} 已成功重启。");
    }
    else
    {
        Console.WriteLine($"未找到名称为 {siteName} 的应用程序池。");
    }
}
catch (Exception ex)
{
    Console.WriteLine($"应用池 {siteName} 重启异常。{ex.Message}");
}

try
{
    // 停止站点（如果正在运行）
    if (site.State == ObjectState.Started)
    {
        site.Stop();
        Console.WriteLine($"站点 '{siteName}' 已停止。");
    }

    // 启动站点
    site.Start();
    Console.WriteLine($"站点 '{siteName}' 已启动。");
}
catch (Exception ex)
{
    Console.WriteLine($"重启站点时出错: {ex.Message}");
}

Console.WriteLine($"网站预加载...");
Thread.Sleep(1000);
 
var cont = new WebScraper().GetPageContentAsync(url).Result;
if (!string.IsNullOrWhiteSpace(cont))
{
    Console.WriteLine($"获取网页内容成功");
}

Thread.Sleep(1000);

